<?php

/**
 * @author Terry
 * @copyright 2009
 */
 
	require_once("class.php");
	session_start();
	
	Action::save();
	$a = $_SESSION['a'];
	$b = $_SESSION['b'];
	
	
	$skillfactory = SkillFactory::get();

	$_SESSION['show'] = "";
	$_SESSION['result'] = "";
	$done = 0;	
	$aorb = 0; //0：A组行动 1：B组行动
	
/** -----------   基本函数   --------------**/

/**
 * 查找给定数组里第一个非空项，返回index
 */
function find_first_set_skill($a)
{
	for($i=0;$i<count($a);$i++)  {
		if($a[$i] !=-1)
			return $i;
	}
	return -1;
} 

function find_first_set_def($a)
{
	for($i=0;$i<count($a);$i++)  {
		if($a[$i] == 1)
			return $i;
	}
	return -1;
} 

/** -----------   传入参数处理   ---------------**/

/** HPSPTP五围处理 **/
	for($i=0;$i<count($a);$i++) {
		$a[$i]->hp = $_POST['ahp'][$i];
		$a[$i]->sp = $_POST['asp'][$i];
		$a[$i]->ap = $_POST['aap'][$i];
		$a[$i]->sap = $_POST['asap'][$i];
		
		$a[$i]->str = $_POST['astr'][$i];
		$a[$i]->mgr = $_POST['amgr'][$i];
		$a[$i]->spd = $_POST['aspd'][$i];
		$a[$i]->vit = $_POST['avit'][$i];
		$a[$i]->luk = $_POST['aluk'][$i];	
	}
	
	for($i=0;$i<count($b);$i++) {
		$b[$i]->hp = $_POST['bhp'][$i];
		$b[$i]->sp = $_POST['bsp'][$i];
		$b[$i]->ap = $_POST['bap'][$i];
		$b[$i]->sap = $_POST['bsap'][$i];
		
		$b[$i]->str = $_POST['bstr'][$i];
		$b[$i]->mgr = $_POST['bmgr'][$i];
		$b[$i]->spd = $_POST['bspd'][$i];
		$b[$i]->vit = $_POST['bvit'][$i];
		$b[$i]->luk = $_POST['bluk'][$i];	
	}
	
	

/** 耐性数组赋值 **/
	for($i=0;$i<count($a);$i++) {
		for($j=0;$j<8;$j++){
			$a[$i]->res[$j] = $_POST['ares_'.$j.$i];
		}
		$a[$i]->res[8] = $_POST['ares_all'.$i];	
	}
	
	for($i=0;$i<count($b);$i++) {
		for($j=0;$j<8;$j++){
			$b[$i]->res[$j] = $_POST['bres_'.$j.$i];
		}
		$b[$i]->res[8] = $_POST['bres_all'.$i];		
	}

/** 被动技能数组赋值 **/
	for($i=0;$i<count($a);$i++) {
		if(isset($_POST['bsa'.$i]))
		foreach($_POST['bsa'.$i] as $id) {			
			$a[$i]->before_status[$id]->on = 1;
		}
		if(isset($_POST['assa'.$i]))
		foreach($_POST['assa'.$i] as $id) {
			$a[$i]->atkskill_status[$id]->on = 1;
		}
		if(isset($_POST['dssa'.$i]))
		foreach($_POST['dssa'.$i] as $id) {			
			$a[$i]->defskill_status[$id]->on = 1;
		}
		if(isset($_POST['aasa'.$i]))
		foreach($_POST['aasa'.$i] as $id) {
			$a[$i]->atkafter_status[$id]->on = 1;
		}
		if(isset($_POST['dasa'.$i]))
		foreach($_POST['dasa'.$i] as $id) {
			$a[$i]->defafter_status[$id]->on = 1;
		}
		if(isset($_POST['essa'.$i]))
		foreach($_POST['essa'.$i] as $id) {
			$a[$i]->eventskill_status[$id]->on = 1;
		}			
	}
	
	for($i=0;$i<count($b);$i++) {
		if(isset($_POST['bsb'.$i]))
		foreach($_POST['bsb'.$i] as $id) {
			$b[$i]->before_status[$id]->on = 1;
		}
		if(isset($_POST['assb'.$i]))
		foreach($_POST['assb'.$i] as $id) {
			$b[$i]->atkskill_status[$id]->on = 1;
		}
		if(isset($_POST['dssb'.$i]))
		foreach($_POST['dssb'.$i] as $id) {		
			$b[$i]->defskill_status[$id]->on = 1;
		}
		if(isset($_POST['aasb'.$i]))
		foreach($_POST['aasb'.$i] as $id) {			
			$b[$i]->atkafter_status[$id]->on = 1;
		}
		if(isset($_POST['dasb'.$i]))
		foreach($_POST['dasb'.$i] as $id) {			
			$b[$i]->defafter_status[$id]->on = 1;
		}
		if(isset($_POST['essb'.$i]))
		foreach($_POST['essb'.$i] as $id) {			
			$b[$i]->eventskill_status[$id]->on = 1;
		}		
	}

/** 获得Attacker及技能ID**/
	$atkid = find_first_set_skill($_POST['suida']);
	if ($atkid != -1) {
		$atker = $a[$atkid];
		$suid = $_POST['suida'][$atkid];
		$aorb = 0;
		$_SESSION['aorb']=$aorb;
	} 
	else {
		$atkid = find_first_set_skill($_POST['suidb']);
		if ($atkid == -1) {
			$_SESSION['result'] = "<font color=red>没有选择技能！</font><br/>";
			header("Location: battle.php");
			die();
		}
		$atker = $b[$atkid];
		$suid = $_POST['suidb'][$atkid];
		$aorb = 1;	
		$_SESSION['aorb']=$aorb;
	}
	
/** 获得defender
  * $defer:单体攻击（或者回复）情况下为目标
  *        扩散情况下为主目标
  *        全体和全场情况下无意义
  */

		
  	$defid = $_POST['atgt'][0][0];
	if ($defid !="") {
		$defer = $a[$defid];
	} 
	else {
		$defid = $_POST['btgt'][0][0];
		if($defid == "")
			$defer = "";
		else
			$defer = $b[$defid];	
	}

/** 获取技能对象 **/
	$skill = $skillfactory->getSkill($suid);
	$s = clone $skill;
	$s->oap = $s->ap;	//记录原始耗费AP
	
/** 对系统操作如回合跳进作特殊处理 **/
	if($s->sysop) {
		$s->start($atker,$defer);
		header("Location: battle.php");
		die();
	}

/** 置行动状态，再动时（眼光系列）状态需要-1! **/	
	if(!$s->nodone) {
		$atker->done += 1;
	}

/** 遍历执行attacker事前状态数组action **/

	foreach($atker->before_status as $bs) {
		if($bs->on == 1) {
			if($done == 0) {
				$done = $bs->start($atker,$defer,$s);
			} else {
				$bs->start($atker,$defer,$s);
			}
		}	
	}

/** 强制行动处理 **/
	switch($atker->forceaction) {
	case 0:{break;}
	//待机
	case 1:{
		$skill = $skillfactory->getSkill(505);
		$s = clone $skill;
		$atker->forceaction = 0;
		break;
	}
	//防御
	case 2:{
		$skill = $skillfactory->getSkill(506);
		$s = clone $skill;		
		$atker->forceaction = 0;
		break;
	}
	//随机普攻
	case 3:{
		$skill = $skillfactory->getSkill(1);
		$s = clone $skill;
		$defer = (mt_rand(0,1) == 0) ? $a[array_rand($a)] : $b[array_rand($b)];
		$atker->forceaction = 0;	
		break;
	}
	//跳过行动
	case 4:{
		$skill = $skillfactory->getSkill(387);
		$s = clone $skill;
		$atker->forceaction = 0;	
		break;
	}
	default:{}
	}

/** 对全体A B进行特殊效果处理：瘟疫 饥饿 

	$c = array_merge($a,$b);
	foreach($c as $t) {
		foreach($t->spec_status as $ss){
			if($ss->on == 1) {
				if($done == 0)
					$done = $ss->action($t);
				else
					$ss->action($t);
			}		
		}
	}
**/

/** 是否行动结束 **/
	
	if ($done == 1) {
		$_SESSION['show'] .= $atker->name."本次行动跳过。<br>";
		$atker->done = 1;
		CalHelper::go_turn(0);
		header("Location: battle.php");
		die();
	}



/** 遍历执行attacker主动技能影响数组action **/
	foreach($atker->atkskill_status as $ass) {
		if($ass->on == 1)
			$ass->action($atker,$s);
	}
		


/** ---------- Action Start!! --------------**/
	
	if($aorb == 0 ) {
		$s->start($a,$b,$atkid,$defid,$defer);
	}	
	else {
		$s->start($b,$a,$atkid,$defid,$defer);
	}

/** 遍历执行attacker事后状态数组action **/
	foreach($atker->atkafter_status as $as) {
		if($as->on == 1)
			$as->action($atker,$s);
	}
    
/** 被说得者置后处理 **/
/*
	$doit=1;
	foreach($a as $aas) {
		if($aas->hp > 0) {$doit = 0;break;}
	}
	if($doit == 1) {
		foreach($b as $bas){
			if($bas->talked == 1){
				if($bas->atkafter_status[6]->on == 1) $bas->atkafter_status[6]->cancel($bas);
				if($bas->atkafter_status[8]->on == 1) $bas->atkafter_status[8]->cancel($bas);
				if($bas->atkafter_status[9]->on == 1) $bas->atkafter_status[9]->cancel($bas);
			}
		}
	}

	$doit=1;
	foreach($b as $bas) {
		if($bas->hp > 0) {$doit = 0;break;}
	}
	if($doit == 1) {
		foreach($a as $aas){
			if($aas->talked == 1){
				if($aas->atkafter_status[6]->on == 1) $aas->atkafter_status[6]->cancel($aas);
				if($aas->atkafter_status[8]->on == 1) $aas->atkafter_status[8]->cancel($aas);
				if($aas->atkafter_status[9]->on == 1) $aas->atkafter_status[9]->cancel($aas);
			}
		}
	}
*/

/** 回合处理 **/
	CalHelper::go_turn(0);

/** ------  结果输出 返回  ----------**/

	header("Location: battle.php");
?>
